# -*- coding: utf-8 -*-
'''
Created on 2010-8-8

@author: morningbzy
'''
import copy
import random

#from sgs.common.constants import cardColor
from sgs.common.constants import cardType, cardColor
from sha import Sha
from shan import Shan
from tao import Tao
from juedou import JueDou
from wuzhongshengyou import WuZhongShengYou
from taoyuanjieyi import TaoYuanJieYi
from shunshouqianyang import ShunShouQianYang
from guohechaiqiao import GuoHeChaiQiao

AVAIL_CARD = {'杀':Sha, '闪':Shan, '桃':Tao, '决斗':JueDou,
    '无中生有':WuZhongShengYou, '桃园结义':TaoYuanJieYi,
    '顺手牵羊':ShunShouQianYang, '过河拆桥': GuoHeChaiQiao,
    
    }
POINT = range(1, 14)
CARD_DEFINE = [
    [cardColor.CLUB, POINT[0], '决斗', '闪电'],
    [cardColor.CLUB, POINT[1], '雌雄双股箭', '八卦阵', '寒冰剑'],
    [cardColor.CLUB, POINT[2], '顺手牵羊', '过河拆桥'],
    [cardColor.CLUB, POINT[3], '顺手牵羊', '过河拆桥'],
    [cardColor.CLUB, POINT[4], '绝影+1', '青龙偃月刀'],
    [cardColor.CLUB, POINT[5], '青虹剑', '乐不思蜀'],
    [cardColor.CLUB, POINT[6], '杀', '南蛮入侵'],
    [cardColor.CLUB, POINT[7], '杀', '杀'],
    [cardColor.CLUB, POINT[8], '杀', '杀'],
    [cardColor.CLUB, POINT[9], '杀', '杀'],
    [cardColor.CLUB, POINT[10], '顺手牵羊', '无懈可击'],
    [cardColor.CLUB, POINT[11], '过河拆桥', '丈八蛇矛'],
    [cardColor.CLUB, POINT[12], '南蛮入侵', '大宛-1'],
    [cardColor.HEART, POINT[0], '万箭齐发', '桃园结义'],
    [cardColor.HEART, POINT[1], '闪', '闪'],
    [cardColor.HEART, POINT[2], '五谷丰登', '桃'],
    [cardColor.HEART, POINT[3], '五谷丰登', '桃'],
    [cardColor.HEART, POINT[4], '赤兔-1', '麒麟弓'],
    [cardColor.HEART, POINT[5], '乐不思蜀', '桃'],
    [cardColor.HEART, POINT[6], '无中生有', '桃'],
    [cardColor.HEART, POINT[7], '无中生有', '桃'],
    [cardColor.HEART, POINT[8], '无中生有', '桃'],
    [cardColor.HEART, POINT[9], '杀', '杀'],
    [cardColor.HEART, POINT[10], '无中生有', '杀'],
    [cardColor.HEART, POINT[11], '桃', '过河拆桥', '闪电'],
    [cardColor.HEART, POINT[12], '闪', '爪黄飞电+1'],
    [cardColor.SPADE, POINT[0], '决斗', '诸葛连弩'],
    [cardColor.SPADE, POINT[1], '杀', '八卦阵', '仁王盾'],
    [cardColor.SPADE, POINT[2], '杀', '过河拆桥'],
    [cardColor.SPADE, POINT[3], '杀', '过河拆桥'],
    [cardColor.SPADE, POINT[4], '杀', '的卢+1'],
    [cardColor.SPADE, POINT[5], '杀', '乐不思蜀'],
    [cardColor.SPADE, POINT[6], '杀', '南蛮入侵'],
    [cardColor.SPADE, POINT[7], '杀', '杀'],
    [cardColor.SPADE, POINT[8], '杀', '杀'],
    [cardColor.SPADE, POINT[9], '杀', '杀'],
    [cardColor.SPADE, POINT[10], '杀', '杀'],
    [cardColor.SPADE, POINT[11], '无懈可击', '借刀杀人'],
    [cardColor.SPADE, POINT[12], '无懈可击', '借刀杀人'],
    [cardColor.DIANMOND, POINT[0], '决斗', '诸葛连弩'],
    [cardColor.DIANMOND, POINT[1], '闪', '闪'],
    [cardColor.DIANMOND, POINT[2], '顺手牵羊', '闪'],
    [cardColor.DIANMOND, POINT[3], '顺手牵羊', '闪'],
    [cardColor.DIANMOND, POINT[4], '杀', '闪'],
    [cardColor.DIANMOND, POINT[5], '杀', '闪'],
    [cardColor.DIANMOND, POINT[6], '杀', '闪'],
    [cardColor.DIANMOND, POINT[7], '杀', '闪'],
    [cardColor.DIANMOND, POINT[8], '杀', '闪'],
    [cardColor.DIANMOND, POINT[9], '杀', '闪'],
    [cardColor.DIANMOND, POINT[10], '闪', '闪'],
    [cardColor.DIANMOND, POINT[11], '方天画戟', '桃', '无懈可击'],
    [cardColor.DIANMOND, POINT[12], '紫驴-1', '闪电'],
]

class CardList:
    def createCard(id, color, point, name):
        cardClass = AVAIL_CARD.get(name, None)
        if cardClass:
            return cardClass(id, color, point)
        else:
            return None
    ALL_CARDS = []
    cardId = 0
    for _ in CARD_DEFINE:
        color, point, names = _[0], _[1], _[2:]
        for name in names:
            c = createCard(cardId, color, point, name)
            if c:
                ALL_CARDS.append(c)
                cardId += 1
            
    def __init__(self):
        self.cardsToUse = range(len(CardList.ALL_CARDS))
        random.shuffle(self.cardsToUse)
        self.cardsInTrash = []
    
    def popCards(self, num):
        if len(self.cardsToUse) <= num:
            random.shuffle(self.cardsInTrash)
            self.cardsToUse.extend(self.cardsInTrash)
            self.cardsInTrash = []
        assert len(self.cardsToUse) > num, 'Not enough cards: required ' + num
        rtn = [CardList.ALL_CARDS[i] for i in self.cardsToUse[0:num]]
        self.cardsToUse = self.cardsToUse[num:]
        return rtn
    
    def trashCard(self, ids):
        if isinstance(ids, list):
            self.cardsInTrash.extend(id)
        else:
            self.cardsInTrash.append(ids)
    
    def get(self, id):
        id = int(id)
        if id < len(CardList.ALL_CARDS):
            return CardList.ALL_CARDS[id]
        return None
    
cardList = CardList()